/**
 * 
 */
package edu.ucr.cs.dblab.acestore.base.index;

import java.io.IOException;
import java.nio.ByteBuffer;

import edu.ucr.cs.dblab.acestore.base.ACEContext;
import edu.ucr.cs.dblab.acestore.base.datatypes.SchemaDescriptor;

/**
 * @author jarod
 * @created Jan 11, 2011
 */
public abstract class AbstractIndex {

	protected ACEContext ctx;
	
	protected final SchemaDescriptor schemaDescriptor;
	
	protected final IIndexPageHelper[] pageHelpers;
	
	protected int idxID;
	
	public AbstractIndex(ACEContext ctx, SchemaDescriptor schemaDescriptor, IIndexPageHelper[] pageHelpers) throws IOException{
		this.ctx = ctx;
		this.schemaDescriptor = schemaDescriptor;
		this.pageHelpers = pageHelpers;
		this.idxID = -1;
		initIndexHead();
	}
	
	public AbstractIndex(ACEContext ctx, SchemaDescriptor schemaDescriptor, IIndexPageHelper[] pageHelpers, int idxID) throws IOException{
		this.ctx = ctx;
		this.schemaDescriptor = schemaDescriptor;
		this.pageHelpers = pageHelpers;
		this.idxID = idxID;
		readIndexHead();
	}
	
	public int[] getTypeList() {
		int[] typeList = new int[pageHelpers.length];
		for(int i = 0; i < typeList.length; i++){
			typeList[i] = pageHelpers[i].getPageType();
		}
		return typeList;
	}
	
	public abstract void readIndexHead () throws IOException;
	
	public abstract void writeIndexHead () throws IOException;
	
	public abstract void initIndexHead () throws IOException;
	
	public abstract ByteBuffer query(byte[] key, int[] keyFields) throws IOException;
	
	public abstract void update(byte[] key, int[] keyFields, byte[] newValue, int[] targetFields) throws IOException;
	
	public abstract void delete(byte[] key, int[] keyFields) throws IOException;
	
	public abstract void insert(byte[] data) throws IOException;
	
}
